<template>
  <el-dialog title="新增权限点" :visible="value" @close="close">
    <el-form :model="form" label-width="100px" :rules="rules" ref="formRef">
      <el-form-item label="权限名称" prop="name">
        <el-input v-model="form.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="权限标识" prop="code">
        <el-input v-model="form.code" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="权限描述">
        <el-input v-model="form.description" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="开启">
        <el-switch v-model="form.enVisible" active-color="#13ce66" inactive-color="#ff4949" active-value="1" inactive-value="0">
        </el-switch>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close()">取 消</el-button>
      <el-button type="primary" @click="submitForm()">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { addPermissionRequest, getPermissionByIdRequest, updatePermissionByIdRequest } from "@/api/permission";
export default {
  name: 'PermissionDialog',
  props: {
    value: {
      type: Boolean,
      default: false,

    },
    sonData: {
      type: Object,
      default: () => { }
    },
    // updateId: {
    //   type: String,
    //   default: null
    // }
  },
  data () {
    return {
      form: {
        name: '',
        code: '',
        description: '',
        //数字或者字符串还有布尔值 默认给 null
        type: null,
        pid: null,
        enVisible: 0
      },
      rules: {
        name: [
          { required: true, message: '请输入权限名称', trigger: 'blur' },
        ],
        code: [
          { required: true, message: '请输入权限标识', trigger: 'blur' },
        ],
      }
    }
  },
  created () {
  },
  computed: {
  },
  methods: {
    close () {
      this.$emit('input', false)
      this.$refs.formRef.resetFields()
      this.form = this.$options.data().form
    },
    submitForm () {
      this.$refs.formRef.validate(async (valid) => {
        if (valid) {
          if (this.form.id) {
            await updatePermissionByIdRequest(this.form)
          } else {
            this.form.type = this.sonData.type
            this.form.pid = this.sonData.pid
            await addPermissionRequest(this.form)
          }

          this.close()
          this.$emit('updata')
        }
      });
    },
    async getPermissionById (id) {
      const res = await getPermissionByIdRequest(id)
      this.form = res.data
    }
  }
}
</script>
<style lang='scss'  scoped>
</style>
